// pages/broadcast/index/select-region/select-region.js
import {authorization} from "../../utils/Util"
import {getLocation} from "../../utils/Wx"
import {getStorageSync, setStorage} from "../../utils/Storage";
import {reverseGeocoder, getRegions} from "../../utils/Location";
Component({

    /**
     * 组件的属性列表
     */
    properties: {
        show: { //是否显示弹框
            type: Boolean,
            value: false
        },
        region: { //区域值
            type: String,
            value: ""
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _regionPop: {
            loading: false,
            show: false,
            list: []
        },
    },

    /*
    * 监听
    * */
    observers: {
        "show": function (value) {
            if (value) {
                this.init()
            }
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {

        /*
        * 定位相关事件
        * */
        init: async function () { //点击cell
            const that = this;
            try {
                let currentRegion = that.data.region;
                if (!currentRegion) { //如果存在区域值  则在点击时候展示弹框
                    if (await authorization("scope.userLocation")) {
                        let location = await getLocation();
                        that.triggerEvent("selectRegion", {name: "loading", result: {}}, {});
                        let res = await reverseGeocoder(location);
                        that.triggerEvent("selectRegion", {
                            name: "success",
                            result: {
                                region: res.region,
                                regionDetail: res.street + res.landmark
                            }
                        }, {});
                    }
                } else {
                    let regions = getStorageSync("regions");
                    if (!regions) {
                        this.setData({
                            "_regionPop.show": true,
                            "_regionPop.loading": true,
                        });
                        regions = await getRegions();
                        this.setData({"_regionPop.loading": false});
                    }
                    this._regions = regions;
                    let regionList = this._getRegionList(currentRegion);
                    that.setData({
                        "_regionPop.list": regionList
                    })
                }
            } catch (e) {
                console.error(e)
            }
        },

        /*
        * 获取区域列表信息
        * */
        _getRegionList: function (currentRegion) {
            let regions = this._regions;
            let fistList, fistInx, firstFullName, secList, secInx, thirdList, thirdInx;
            let currentArr = currentRegion.split(",")
            fistList = regions[0];
            fistInx = fistList.findIndex(function (item) {
                return item.fullname === currentArr[0]
            })
            firstFullName = fistList[fistInx].fullname;
            if (firstFullName.indexOf("市") !== -1) { //直辖市
                secList = [fistList[fistInx]];
                secInx = 0;
                thirdList = regions[1].slice(secList[secInx].cidx[0], secList[secInx].cidx[1] + 1);
                thirdInx = currentArr[2] ? thirdList.findIndex(function (item) {
                    return item.fullname === currentArr[2]
                }) : 0
            } else {
                secList = regions[1].slice(fistList[fistInx].cidx[0], fistList[fistInx].cidx[1] + 1);
                secInx = currentArr[1] ? secList.findIndex(function (item) {
                    return item.fullname === currentArr[1]
                }) : 0
                thirdList = regions[2].slice(secList[secInx].cidx[0], secList[secInx].cidx[1] + 1);
                thirdInx = currentArr[2] ? thirdList.findIndex(function (item) {
                    return item.fullname === currentArr[2]
                }) : 0
            }
            return [{
                values: fistList,
                defaultIndex: fistInx
            }, {
                values: secList,
                defaultIndex: secInx
            }, {
                values: thirdList,
                defaultIndex: thirdInx
            }]
        },


        onRegionChange: function (e) { //单列变动
            let index = e.detail.index;
            let value = e.detail.value;
            let currentRegion = "";
            for (let i = 0; i <= index; i ++) {
                if (i == 0) {
                    currentRegion = value[i].fullname;
                } else {
                    currentRegion = (currentRegion + "," + value[i].fullname)
                }
            }
            let list = this._getRegionList(currentRegion);
            this.setData({
                "_regionPop.list": list
            })
        },

        onRegionConfirm: function (e) { //区域选择组件确定按钮
            const that = this;
            let valueLst = e.detail.value;
            let currentRegion = that.data.region
            let value = valueLst[0].fullname + "," + valueLst[1].fullname + "," + valueLst[2].fullname;
            if (currentRegion !== value) { //有变动
                that.triggerEvent("selectRegion", {
                    name: "success",
                    result: {
                        region: value,
                        regionDetail: ""
                    }
                }, {});
            }
            that.setData({"_regionPop.show": false})
        },
        onRegionClose: function () { //关闭弹框
            this.setData({
                "_regionPop.show": false
            })
        }
    }
})
